package org.info.service;

import java.util.List;

import org.info.dto.QueryExcution;
import org.info.enums.JsgxMobileDataStateEnum;
import org.info.exception.JsgxMobileDataException;
import org.info.mapper.JsgxMobileDataDao;
import org.info.mapper.cache.RedisDao;
import org.info.model.JsgxMobileData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class JsgxMobileDataService {

	private Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	private JsgxMobileDataDao jsgxMobileDataDao;
	
	@Autowired
	private RedisDao redisDao;
	
	public JsgxMobileData getById(String saleAttr, String typeId) {
		return jsgxMobileDataDao.queryById(saleAttr, typeId);
	}
	
	public QueryExcution getList(String saleAttr, String typeId)
		throws JsgxMobileDataException{
		
		try {
			// 1 访问 Redis
			List<JsgxMobileData> list = redisDao.getJsgxMobileData(saleAttr, typeId);
			
			if (list == null || list.size() == 0) {
				
				// 2 访问数据库
				list = jsgxMobileDataDao.queryList(saleAttr, typeId);
				if (list.size() == 0) {
					throw new JsgxMobileDataException("【数据查询】:数据不存在");
				} else {
					redisDao.putJsgxMobileData(list);
					logger.info("【数据缓存】:{}", list);
				}
			} else {
				logger.info("【数据】:{}",list);
			}
			return new QueryExcution(typeId, saleAttr, JsgxMobileDataStateEnum.SUCCESS, list);
		} catch (JsgxMobileDataException e) {
			throw e;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new JsgxMobileDataException("inner error:" + e.getMessage());
		}
	}
}
